#!/usr/sbin/perl
#
#  Copyright (C) 1998-2000, Silicon Graphics, Inc.
#  All Rights Reserved.
#
#  UNPUBLISHED -- Rights reserved under the copyright laws of the United
#  States.  Use of a copyright notice is precautionary only and does not
#  imply publication or disclosure.
#
#  THIS SOFTWARE CONTAINS CONFIDENTIAL AND PROPRIETARY INFORMATION OF
#  SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION, DISTRIBUTION, OR
#  DISCLOSURE IS STRICTLY PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN
#  PERMISSION OF SILICON GRAPHICS, INC.
#
#  U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
#  Use, duplication or disclosure by the Government is subject to
#  restrictions as set forth in FAR 52.227.19(c)(2) or subparagraph
#  (c)(1)(ii) of the Rights in Technical Data and Computer Software
#  clause at DFARS 252.227-7013 and/or in similar or successor clauses
#  in the FAR, or the DOD or NASA FAR Supplement.  Unpublished-- rights
#  reserved under the copyright laws of the United States.
#  Contractor/manufacturer is Silicon Graphics, Inc.,
#  2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
#

# PerformVolumeTest
#
# Get all resources for a particular type
# and run the tests against that resource.

require "$LibDiagsPath/constants.pl";
require "$LibDiagsPath/libdiags.pl";
require "$LibDiagsPath/recurse.pl";
use POSIX;

sub volume {
  local ($machine_name, $resource) = @_;
  local ($this);
  local (@list);
  local ($retv)=$CD_SUCCESS;

  &LogEntry();
  &Verbose("Running resource type %s tests on node %s",
	   $VOLUME, $machine_name);

  if ($resource eq "ALL") {
    @list=&GetResFromResType($machine_name, $VOLUME);
    if (scalar(@list) < 1) {
      &Failure("No resources found for resource type %s",$VOLUME);
      return $CD_FAILURE;
    }
    foreach $this (@list) {
       $retv=&TestVolume($machine_name,$this);

       # If the DESTRUCTIVE flag is set ...
       if ($DESTRUCTIVE) {
	   if (&Execvolume($machine_name,$this) == $CD_FAILURE) {
	       &Failure("Resource %s of resource resource type %s failed",$this,
			$VOLUME);
	       $retv=$CD_FAILURE;
	   }
       }
       &ExitStatus($retv);
    }
  }else {
    # We have been given the resource name.
    $retv=&TestVolume($machine_name, $resource);

    # If the DESTRUCTIVE flag is set ...
    if ($DESTRUCTIVE) {
	if (&Execvolume($machine_name,$resource) == $CD_FAILURE) {
	    &Failure("Resource %s of type %s failed",$resource,
		     $VOLUME);
	    $retv=$CD_FAILURE;
	}
    }
    &ExitStatus($retv);
  }
  return $retv;
}

sub Execvolume {
    local ($node, $resource) = @_;
    local ($retv);

    &LogEntry();

    &Status("Starting resource %s of resource type %s on node %s",
	     $resource,$VOLUME,$node);

    # Only execute the destructive tests on this node.
    if (!&OnTheSameNode($node)) {
	&Notice("Can only execute the resource destructive tests on local host");
	&Log("Can only execute the resource destructive tests on local host %s/%s",
	      $this_host, $NodeToHost{$node});
	return $CD_SUCCESS;
    }

    &Status("Executing the $VOLUME resource scripts, I.e. probe, exclusive, start, monitor and stop");
    &Status("Please be patient ...");
    foreach $script (@HA_LIST) {
	$retv=ExecuteScript($node,$resource,$VOLUME,$script);
	if ($retv == 0) {
	    # script failed, exit
	    return $CD_FAILURE;
	}
	# Sleep a while
	sleep(3);
    }
    return $CD_SUCCESS;
}

# TestVolume
# The resource test has three parts
#   1) Check the resource parameters.
#   2) If the resource has dependencies check them.

sub TestVolume {
    local ($machine, $volume) = @_;
    local ($status);
    local ($cmd);
    local ($retv);
    local ($mode,$group,$owner,@parameters);

    $retv=$CD_SUCCESS;

    &LogEntry();
    &Status("Testing resource %s of resource type %s on node %s",
	     $volume,$VOLUME,$machine);

    # Check the resource attributes.
    @parameters=&GetResParameters($machine_name,$VOLUME,$volume);
    if (scalar(@parameters) < 1 ) {
      &Failure("No parameters returned for resource %s of resource type %s",
	       $resource,$VOLUME);
      return $CD_FAILURE;
    }
    if ($parameters[0] < 0){
      &Failure("No parameters found for resource %s of resource type %s",
	       $resource,$VOLUME);
      return $CD_FAILURE;
    }

    if (($owner=&pulloff("devname-owner",@parameters)) eq "") {
      &Failure("%s resource %s does not have the %s parameter set",
	       $VOLUME,$volume,"devname-owner");
      $retv=$CD_FAILURE;
    }

    if (($group=&pulloff("devname-group",@parameters)) eq "") {
      &Failure("%s resource %s does not have the %s parameter set",
	       $VOLUME,$volume,"devname-group");
      $retv=$CD_FAILURE;
    }

    if (($mode=&pulloff("devname-mode",@parameters)) eq "") {
      &Failure("%s resource %s does not have the %s parameter set",
	       $VOLUME,$volume,"devname-mode");
      $retv=$CD_FAILURE;
    }

    # Add simple tests, tests taken from FailSafe 1.2.

    # Check if xlv_mgr is present. The volumes script uses
    # xlv_mgr. This is installed using a xlv patch. If the
    # particular xlv patch is not installed, HA will fail.
    $cmd="ls -l /sbin/xlv_mgr > /dev/null";
    @output=&rsh($machine,$cmd);
    if ($RSH_ERRNO != 0) {
       &Failure("/sbin/xlv_mgr command not present on node %s",$machine);
       # No point continuing ...
       return $CD_FAILURE;
    }

    $cmd="/sbin/xlv_mgr -Rc \"show config\" > /dev/null";
    @output=&rsh($machine,$cmd);
    if ($RSH_ERRNO != 0) {
       &Failure("Some xlv patches have not been installed on the system");
       $retv=$CD_FAILURE;
    }

    # Check that the volume exists ...
    $cmd="/sbin/xlv_mgr -c\"show -short $volume\" > /dev/null";
    @output=&rsh($machine,$cmd);
    if ($RSH_ERRNO != 0) {
      &Failure("Resource %s of resource type %s has not been created or does not exist on node %s",
	       $volume,$VOLUME,$machine);
      $retv=$CD_FAILURE;
    }

    # Check for the plexing licence
    $cmd="/sbin/xlv_mgr -Rc \"show config\" | grep \"Plexing license\" | grep \"not\"";
    @output=&rsh($machine,$cmd);
    if ($RSH_ERRNO == 0) {
      &Warning("Check if XLV license has been installed on node %s",
	       $machine);
      $retv=$CD_SUCCESS;
    }
    return ($retv);
}
